package com.eda.db;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

import android.content.Context;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.text.TextUtils;
import android.util.Log;

public class DatabaseHelper extends SQLiteOpenHelper {
	
	private static final String DATABASE_NAME = "basedata.db";
	private static final int DATABASE_VERSION = 1;
	InputStream in;
	
	public DatabaseHelper(Context context) {
		super(context, DATABASE_NAME, null, DATABASE_VERSION);
		try{
			in = context.getResources().getAssets().open("city.sql");
		}catch(Exception e){
			
		}
	}

	@Override
	public void onCreate(SQLiteDatabase db) {
		String sql = "CREATE TABLE province ( codeid INTEGER null, "
				+ "parentid INTEGER null, " + "cityName text not null);";
		Log.i("haiyang:createDB=", sql);
		db.execSQL(sql);

		sql = "CREATE TABLE truck ( truckid INTEGER null, "
				+ "name text not null);";
		db.execSQL(sql);
		try {
			BufferedReader bufferedReader = new BufferedReader(
					new InputStreamReader(in));
			String sqlUpdate = null;
			while ((sqlUpdate = bufferedReader.readLine()) != null) {
				if (!TextUtils.isEmpty(sqlUpdate)) {
					db.execSQL(sqlUpdate);
				}
			}
			bufferedReader.close();
			in.close();
		} catch (SQLException e) {
			Log.i("DB init error", e.getMessage());
		} catch (IOException e) {
			Log.i("DB init error", e.getMessage());
		}

	}

	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
	}
}